﻿/**
 * jQuery EasyUI 1.4
 * 
 * Copyright (c) 2009-2014 www.jeasyui.com. All rights reserved.
 * 
 * Licensed under the GPL license: http://www.gnu.org/licenses/gpl.txt To use it
 * on other terms please contact us at info@jeasyui.com
 * 
 */
(function($) {
	$.fn._remove = function() {
		return this.each(function() {
			$(this).remove();
			try {
				this.outerHTML = "";
			} catch (err) {
			}
		});
	};
	function removeNode(target) {
		target._remove();
	};
	function resizePanel(target, options) {
		var panel = $.data(target, "panel");
		var opts = panel.options;
		var ppanel = panel.panel;
		var panelHeader = ppanel.children("div.panel-header");
		var panelBody = ppanel.children("div.panel-body");
		if (options) {
			$.extend(opts, {
				width : options.width,
				height : options.height,
				minWidth : options.minWidth,
				maxWidth : options.maxWidth,
				minHeight : options.minHeight,
				maxHeight : options.maxHeight,
				left : options.left,
				top : options.top
			});
		}
		ppanel._size(opts);
		panelHeader.add(panelBody)._outerWidth(ppanel.width());
		if (!isNaN(parseInt(opts.height))) {
			panelBody._outerHeight(ppanel.height() - panelHeader._outerHeight());
		} else {
			panelBody.css("height", "");
			var minHeight = $.parser.parseValue("minHeight", opts.minHeight, ppanel.parent());
			var maxHeight = $.parser.parseValue("maxHeight", opts.maxHeight, ppanel.parent());
			var outerHeight = panelHeader._outerHeight() + ppanel._outerHeight() - ppanel.height();
			panelBody._size("minHeight", minHeight ? (minHeight - outerHeight) : "");
			panelBody._size("maxHeight", maxHeight ? (maxHeight - outerHeight) : "");
		}
		ppanel.css({
			height : "",
			minHeight : "",
			maxHeight : "",
			left : opts.left,
			top : opts.top
		});
		opts.onResize.apply(target, [ opts.width, opts.height ]);
		$(target).panel("doLayout");
	};
	function movePanel(target, options) {
		var opts = $.data(target, "panel").options;
		var panel = $.data(target, "panel").panel;
		if (options) {
			if (options.left != null) {
				opts.left = options.left;
			}
			if (options.top != null) {
				opts.top = options.top;
			}
		}
		panel.css({
			left : opts.left,
			top : opts.top
		});
		opts.onMove.apply(target, [ opts.left, opts.top ]);
	};
	function wrapPanel(target) {
		$(target).addClass("panel-body")._size("clear");
		var panel = $("<div class=\"panel\"></div>").insertBefore(target);
		panel[0].appendChild(target);
		panel.bind("_resize", function(e, _bl) {
			if ($(this).hasClass("easyui-fluid") || _bl) {
				resizePanel(target);
			}
			return false;
		});
		return panel;
	};
	function setBorder(target) {
		var panel = $.data(target, "panel");
		var opts = panel.options;
		var pp = panel.panel;
		pp.css(opts.style);
		pp.addClass(opts.cls);
		setHeader();
		var panelHeader = $(target).panel("header");
		var panelBody = $(target).panel("body");
		if (opts.border) {
			panelHeader.removeClass("panel-header-noborder");
			panelBody.removeClass("panel-body-noborder");
		} else {
			panelHeader.addClass("panel-header-noborder");
			panelBody.addClass("panel-body-noborder");
		}
		panelHeader.addClass(opts.headerCls);
		panelBody.addClass(opts.bodyCls);
		$(target).attr("id", opts.id || "");
		if (opts.content) {
			$(target).panel("clear");
			$(target).html(opts.content);
			$.parser.parse($(target));
		}
		function setHeader() {
			if (opts.tools && typeof opts.tools == "string") {
				pp.find(">div.panel-header>div.panel-tool .panel-tool-a").appendTo(opts.tools);
			}
			removeNode(pp.children("div.panel-header"));
			if (opts.title && !opts.noheader) {
				var header = $("<div class=\"panel-header\"></div>").prependTo(pp);
				var title = $("<div class=\"panel-title\"></div>").html(opts.title).appendTo(header);
				if (opts.iconCls) {
					title.addClass("panel-with-icon");
					$("<div class=\"panel-icon\"></div>").addClass(opts.iconCls).appendTo(header);
				}
				var tool = $("<div class=\"panel-tool\"></div>").appendTo(header);
				tool.bind("click", function(e) {
					e.stopPropagation();
				});
				if (opts.tools) {
					if ($.isArray(opts.tools)) {
						for (var i = 0; i < opts.tools.length; i++) {
							var t = $("<a href=\"javascript:void(0)\"></a>").addClass(opts.tools[i].iconCls).appendTo(tool);
							if (opts.tools[i].handler) {
								t.bind("click", eval(opts.tools[i].handler));
							}
						}
					} else {
						$(opts.tools).children().each(function() {
							$(this).addClass($(this).attr("iconCls")).addClass("panel-tool-a").appendTo(tool);
						});
					}
				}
				if (opts.collapsible) {
					$("<a class=\"panel-tool-collapse\" href=\"javascript:void(0)\"></a>").appendTo(tool).bind("click", function() {
						if (opts.collapsed == true) {
							expandPanel(target, true);
						} else {
							collapsePanel(target, true);
						}
						return false;
					});
				}
				if (opts.minimizable) {
					$("<a class=\"panel-tool-min\" href=\"javascript:void(0)\"></a>").appendTo(tool).bind("click", function() {
						minimizePanel(target);
						return false;
					});
				}
				if (opts.maximizable) {
					$("<a class=\"panel-tool-max\" href=\"javascript:void(0)\"></a>").appendTo(tool).bind("click", function() {
						if (opts.maximized == true) {
							restorePanel(target);
						} else {
							maximizePanel(target);
						}
						return false;
					});
				}
				if (opts.closable) {
					$("<a class=\"panel-tool-close\" href=\"javascript:void(0)\"></a>").appendTo(tool).bind("click", function() {
						closePanel(target);
						return false;
					});
				}
				pp.children("div.panel-body").removeClass("panel-body-noheader");
			} else {
				pp.children("div.panel-body").addClass("panel-body-noheader");
			}
		};
	};
	function refreshPanel(target, queryParams) {
		var panel = $.data(target, "panel");
		var opts = panel.options;
		if (queryParams) {
			opts.queryParams = queryParams;
		}
		if (!opts.href && !opts.iframe) {
			return;
		}
		if (!panel.isLoaded || !opts.cache) {
			var optsQueryParams=opts.queryParams;
			if(typeof(opts.queryParams)=="function"){
				optsQueryParams=opts.queryParams();
			}
			var param={};
			if(optsQueryParams){
				if(typeof(optsQueryParams)=="object"){
					param=$.extend({},optsQueryParams);
					param.queryString=$.parser.objConvertStr(optsQueryParams);
				}else{
					param=optsQueryParams;
				}
			}
			if (opts.onBeforeLoad.call(target, param) == false) {
				return;
			}
			panel.isLoaded = false;
			$(target).panel("clear");
			if (opts.loadingMessage) {
				$(target).html($("<div class=\"panel-loading\"></div>").html(opts.loadingMessage));
			}
			opts.loader.call(target, param, function(data) {
				$(target).html(opts.extractor.call(target, data));
				$.parser.parse($(target));
				opts.onLoad.apply(target, [$(target)]);
				panel.isLoaded = true;
			}, function() {
				opts.onLoadError.apply(target, [$(target)]);
			},function(){
				$(target).find(".panel-loading").remove();
				opts.onIfarmeLoad.apply(target, [$(target)]);
			});
		}
	};
	function clear(target) {
		var t = $(target);
		t.find(".combo-f").each(function() {
			$(this).combo("destroy");
		});
		t.find(".m-btn").each(function() {
			$(this).menubutton("destroy");
		});
		t.find(".s-btn").each(function() {
			$(this).splitbutton("destroy");
		});
		t.find(".tooltip-f").each(function() {
			$(this).tooltip("destroy");
		});
		t.children("div").each(function() {
			$(this)._size("unfit");
		});
		t.empty();
	};
	function setResizable(target) {
		$(target).panel("doLayout", true);
	};
	function openPanel(target, forceOpen) {
		var opts = $.data(target, "panel").options;
		var panel = $.data(target, "panel").panel;
		if (forceOpen != true) {
			if (opts.onBeforeOpen.call(target) == false) {
				return;
			}
		}
		panel.show();
		opts.closed = false;
		opts.minimized = false;
		var restore = panel.children("div.panel-header").find("a.panel-tool-restore");
		if (restore.length) {
			opts.maximized = true;
		}
		opts.onOpen.call(target);
		if (opts.maximized == true) {
			opts.maximized = false;
			maximizePanel(target);
		}
		if (opts.collapsed == true) {
			opts.collapsed = false;
			collapsePanel(target);
		}
		if (!opts.collapsed) {
			refreshPanel(target);
			setResizable(target);
		}
	};
	function closePanel(target, forceClose) {
		var opts = $.data(target, "panel").options;
		var panel = $.data(target, "panel").panel;
		if (forceClose != true) {
			if (opts.onBeforeClose.call(target) == false) {
				return;
			}
		}
		panel._size("unfit");
		panel.hide();
		opts.closed = true;
		if(opts.clear){
			$(target).panel("clear");
		}
		opts.onClose.call(target);
	};
	function destroyPanel(target, forceDestroy) {
		var opts = $.data(target, "panel").options;
		var panel = $.data(target, "panel").panel;
		if (forceDestroy != true) {
			if (opts.onBeforeDestroy.call(target) == false) {
				return;
			}
		}
		$(target).panel("clear");
		removeNode(panel);
		opts.onDestroy.call(target);
	};
	function collapsePanel(target, animate) {
		var opts = $.data(target, "panel").options;
		var panel = $.data(target, "panel").panel;
		var panelBody = panel.children("div.panel-body");
		var panelHeader = panel.children("div.panel-header").find("a.panel-tool-collapse");
		if (opts.collapsed == true) {
			return;
		}
		panelBody.stop(true, true);
		if (opts.onBeforeCollapse.call(target) == false) {
			return;
		}
		panelHeader.addClass("panel-tool-expand");
		if (animate == true) {
			panelBody.slideUp("normal", function() {
				opts.collapsed = true;
				opts.onCollapse.call(target);
			});
		} else {
			panelBody.hide();
			opts.collapsed = true;
			opts.onCollapse.call(target);
		}
	};
	function expandPanel(target, animate) {
		var opts = $.data(target, "panel").options;
		var panel = $.data(target, "panel").panel;
		var panelBody = panel.children("div.panel-body");
		var panelHeader = panel.children("div.panel-header").find("a.panel-tool-collapse");
		if (opts.collapsed == false) {
			return;
		}
		panelBody.stop(true, true);
		if (opts.onBeforeExpand.call(target) == false) {
			return;
		}
		panelHeader.removeClass("panel-tool-expand");
		if (animate == true) {
			panelBody.slideDown("normal", function() {
				opts.collapsed = false;
				opts.onExpand.call(target);
				refreshPanel(target);
				setResizable(target);
			});
		} else {
			panelBody.show();
			opts.collapsed = false;
			opts.onExpand.call(target);
			refreshPanel(target);
			setResizable(target);
		}
	};
	function maximizePanel(target) {
		var opts = $.data(target, "panel").options;
		var panel = $.data(target, "panel").panel;
		var panelHeader = panel.children("div.panel-header").find("a.panel-tool-max");
		if (opts.maximized == true) {
			return;
		}
		panelHeader.addClass("panel-tool-restore");
		if (!$.data(target, "panel").original) {
			$.data(target, "panel").original = {
				width : opts.width,
				height : opts.height,
				left : opts.left,
				top : opts.top,
				fit : opts.fit
			};
		}
		opts.left = 0;
		opts.top = 0;
		opts.fit = true;
		resizePanel(target);
		opts.minimized = false;
		opts.maximized = true;
		opts.onMaximize.call(target);
	};
	function minimizePanel(target) {
		var opts = $.data(target, "panel").options;
		var panel = $.data(target, "panel").panel;
		panel._size("unfit");
		panel.hide();
		opts.minimized = true;
		opts.maximized = false;
		opts.onMinimize.call(target);
	};
	function restorePanel(target) {
		var opts = $.data(target, "panel").options;
		var panel = $.data(target, "panel").panel;
		var panelHeader = panel.children("div.panel-header").find("a.panel-tool-max");
		if (opts.maximized == false) {
			return;
		}
		panel.show();
		panelHeader.removeClass("panel-tool-restore");
		$.extend(opts, $.data(target, "panel").original);
		resizePanel(target);
		opts.minimized = false;
		opts.maximized = false;
		$.data(target, "panel").original = null;
		opts.onRestore.call(target);
	};
	function setTitle(target, title) {
		$.data(target, "panel").options.title = title;
		$(target).panel("header").find("div.panel-title").html(title);
	};
	var resized = null;
	$(window).unbind(".panel").bind("resize.panel", function() {
		if (resized) {
			clearTimeout(resized);
		}
		resized = setTimeout(function() {
			var layout = $("body.layout");
			if (layout.length) {
				layout.layout("resize");
			} else {
				$("body").panel("doLayout");
			}
			resized = null;
		}, 100);
	});
	$.fn.panel = function(method, options) {
		if (typeof method == "string") {
			return $.fn.panel.methods[method](this, options);
		}
		method = method || {};
		return this.each(function() {
			var panel = $.data(this, "panel");
			var opts;
			if (panel) {
				opts = $.extend(panel.options, method);
				panel.isLoaded = false;
			} else {
				opts = $.extend({}, $.fn.panel.defaults, $.fn.panel.parseOptions(this), method);
				$(this).attr("title", "");
				panel = $.data(this, "panel", {
					options : opts,
					panel : wrapPanel(this),
					isLoaded : false
				});
			}
			setBorder(this);
			if (opts.doSize == true) {
				panel.panel.css("display", "block");
				resizePanel(this);
			}
			if (opts.closed == true || opts.minimized == true) {
				panel.panel.hide();
			} else {
				openPanel(this);
			}
		});
	};
	$.fn.panel.methods = {
		options : function(jq) {
			return $.data(jq[0], "panel").options;
		},
		panel : function(jq) {
			return $.data(jq[0], "panel").panel;
		},
		getIframe : function(jq) {
			return $.data(jq[0], "panel").options.iframeObject;
		},
		header : function(jq) {
			return $.data(jq[0], "panel").panel.find(">div.panel-header");
		},
		body : function(jq) {
			return $.data(jq[0], "panel").panel.find(">div.panel-body");
		},
		setTitle : function(jq, title) {
			return jq.each(function() {
				setTitle(this, title);
			});
		},
		open : function(jq, forceOpen) {
			return jq.each(function() {
				openPanel(this, forceOpen);
			});
		},
		close : function(jq, forceClose) {
			return jq.each(function() {
				closePanel(this, forceClose);
			});
		},
		destroy : function(jq, forceDestroy) {
			return jq.each(function() {
				destroyPanel(this, forceDestroy);
			});
		},
		clear : function(jq) {
			return jq.each(function() {
				clear(this);
			});
		},
		refresh : function(jq, href) {
			return jq.each(function() {
				var panel = $.data(this, "panel");
				panel.isLoaded = false;
				if (href) {
					if (typeof href == "string") {
						panel.options.href = href;
					} else {
						panel.options.queryParams = href;
					}
				}
				refreshPanel(this);
			});
		},
		refreshIframe : function(jq, iframe) {
			return jq.each(function() {
				var panel = $.data(this, "panel");
				panel.isLoaded = false;
				if (iframe) {
					if (typeof iframe == "string") {
						panel.options.iframe = iframe;
					} 
				}
				refreshPanel(this);
			});
		},
		resize : function(jq, options) {
			return jq.each(function() {
				resizePanel(this, options);
			});
		},
		doLayout : function(jq, all) {
			return jq.each(function() {
						var _6a = this;
						var body = _6a == $("body")[0];
						var s = $(this).find("div.panel:visible,div.accordion:visible,div.tabs-container:visible,div.layout:visible,.easyui-fluid:visible").filter(function(_6c, el) {
							var p = $(el).parents("div.panel-body:first");
							if (body) {
								return p.length == 0;
							} else {
								return p[0] == _6a;
							}
						});
						s.trigger("_resize", [ all || false ]);
					});
		},
		move : function(jq, options) {
			return jq.each(function() {
				movePanel(this, options);
			});
		},
		maximize : function(jq) {
			return jq.each(function() {
				maximizePanel(this);
			});
		},
		minimize : function(jq) {
			return jq.each(function() {
				minimizePanel(this);
			});
		},
		restore : function(jq) {
			return jq.each(function() {
				restorePanel(this);
			});
		},
		collapse : function(jq, animate) {
			return jq.each(function() {
				collapsePanel(this, animate);
			});
		},
		expand : function(jq, animate) {
			return jq.each(function() {
				expandPanel(this, animate);
			});
		}
	};
	$.fn.panel.parseOptions = function(target) {
		var t = $(target);
		return $.extend({}, $.parser.parseOptions(target, [ "id", "width",
				"height", "left", "top", "title", "iconCls", "cls",
				"headerCls", "bodyCls", "tools", "href", "method", {
					cache : "boolean",
					fit : "boolean",
					border : "boolean",
					noheader : "boolean"
				}, {
					collapsible : "boolean",
					minimizable : "boolean",
					maximizable : "boolean"
				}, {
					closable : "boolean",
					collapsed : "boolean",
					minimized : "boolean",
					maximized : "boolean",
					closed : "boolean"
				} ]), {
			loadingMessage : (t.attr("loadingMessage") != undefined ? t.attr("loadingMessage") : undefined)
		});
	};
	$.fn.panel.defaults = {
		id : null,
		title : null,
		iconCls : null,
		width : "auto",
		height : "auto",
		left : null,
		top : null,
		cls : null,
		headerCls : null,
		bodyCls : null,
		style : {},
		href : null,
		cache : true,
		fit : false,
		border : true,
		doSize : true,
		noheader : false,
		content : null,
		collapsible : false,
		minimizable : false,
		maximizable : false,
		closable : false,
		collapsed : false,
		minimized : false,
		maximized : false,
		closed : false,
		clear:false,
		tools : null,
		queryParams : {},
		method : "get",
		href : null,
		iframe: null,//iframe url
		iframeObject: null,//iframe url
		internalForm:null,
		loadingMessage : "Loading...",
		loader : function(data, successCall, callError,ifarmeCall) {
			var opts = $(this).panel("options");
			if (opts.iframe) {
				opts.iframeObject=document.createElement("iframe");
				opts.iframeObject.setAttribute('name', "_internal_iframe_post_id_"+this.id);
				opts.iframeObject.setAttribute('frameborder', "no");
				opts.iframeObject.setAttribute('border', "0");
				opts.iframeObject.setAttribute('marginwidth', "0");
				opts.iframeObject.setAttribute('marginheight', "0");
				opts.iframeObject.setAttribute('scrolling', "no");
				opts.iframeObject.setAttribute('allowtransparency', "yes");
				if(opts.width){
					if(opts.width=="auto"){
						opts.iframeObject.setAttribute('width', "100%");
					}else{
						opts.iframeObject.setAttribute('width', opts.width);
					}
				}
				if(opts.height){
					if(opts.height=="auto"){
						opts.iframeObject.setAttribute('height', "100%");
					}else{
						opts.iframeObject.setAttribute('height', opts.height);
					}
				}
			
				this.appendChild(opts.iframeObject);
				$(opts.iframeObject).load(function(){
					ifarmeCall();
                });
				if(data){
					if(typeof(data)=="object"){
						if(opts.iframe.lastIndexOf("?")>0){
							opts.iframeObject.src=opts.iframe+"&"+$.param(data,true);
						}else{
							opts.iframeObject.src=opts.iframe+"?"+$.param(data,true);
						}
						
					}else if(typeof(data)=="string"){
						if(opts.iframe.lastIndexOf("?")>0){
							opts.iframeObject.src=opts.iframe+"&"+data;
						}else{
							opts.iframeObject.src=opts.iframe+"?"+data;
						}
					}else{
						opts.iframeObject.src=opts.iframe;
					}
				}else{
					opts.iframeObject.src=opts.iframe;
				}
				
			}else{
				if (!opts.href) {
					return false;
				}
				$.ajax({
					type : opts.method,
					url : opts.href,
					cache : false,
					data : data,
					dataType : "html",
					success : function(data) {
						successCall(data);
					},
					error : function() {
						callError.apply(this, arguments);
					}
				});
			}
		},
		extractor : function(data) {
			var pattern = /<body[^>]*>((.|[\n\r])*)<\/body>/im;
			var matches = pattern.exec(data);
			if (matches) {
				return matches[1];
			} else {
				return data;
			}
		},
		onBeforeLoad : function(_74) {},
		onLoad : function() {},
		onLoadError : function() {},
		onBeforeOpen : function() {},
		onOpen : function() {},
		onBeforeClose : function() {},
		onClose : function() {},
		onBeforeDestroy : function() {},
		onDestroy : function() {},
		onResize : function(_6c, _6d) {},
		onMove : function(_6e, top) {},
		onMaximize : function() {},
		onRestore : function() {},
		onMinimize : function() {},
		onBeforeCollapse : function() {},
		onBeforeExpand : function() {},
		onCollapse : function() {},
		onExpand : function() {},
		onIfarmeLoad : function() {}
	};
})(jQuery);
